# -*- coding: utf-8 -*-
from chee.shortcuts import *
from chee.models import User_Info
############################################################################
class User_Mail(object):
    __storm_table__ = "user_mail"
    id = Int(primary=True)
    owner_id = Int(default=0)
    owner = Reference(owner_id,User_Info.id)
    from_id = Int(default=0)
    from_user = Reference(from_id,User_Info.id)
    to_id = Int(default=0)
    to_user = Reference(to_id,User_Info.id)
    title = Unicode(default=u'')
    content = Unicode(default=u'')
    send_time = DateTime(default=datetime.max)
    is_read = Int(default=0)
    @classmethod
    def hasNewMail(cls,user_id):
        return bool(store.find(User_Mail,User_Mail.owner_id==user_id,User_Mail.to_id==user_id,User_Mail.is_read==0
            ).count())
class User_Friend(object):
    __storm_table__ = "user_friend"
    id = Int(primary=True)
    owner_id = Int(default=0)
    owner = Reference(owner_id,User_Info.id)
    friend_id = Int(default=0)
    friend = Reference(friend_id,User_Info.id)
    @classmethod
    def isFriend(cls,owner_id,friend_id):
        return bool(store.find(User_Friend,User_Friend.owner_id==owner_id,User_Friend.friend_id==friend_id
                 ).count())
    @classmethod
    def isMyFriend(cls,owner_id,friend_id):
        return cls.isFriend(owner_id, friend_id)

class User_Role(object):
    __storm_table__ = "user_role"
    id = Int(primary=True)
    name = Unicode(default=u'')
    def getUsers(self):
        from chee.models import User_Info
        tbs = [User_Info,Join(User_Info_Role,User_Info_Role.user_id==User_Info.id)]
        return store.using(*tbs).find(User_Info,User_Info_Role.role_id==self.id).order_by(Asc(User_Info.id))
    def getRolePerms(self):
        return store.find(User_Role_Perm,User_Role_Perm.role_id==self.id).order_by(Asc(User_Role_Perm.id))

class User_Perm(object):
    __storm_table__ = "user_permission"
    id = Int(primary=True)
    name = Unicode(default=u'')
    def getOpNames(self):
        tbs = [User_Perm_Op,Join(User_Op,User_Op.id==User_Perm_Op.operation_id)]
        ops = store.using(*tbs).find(User_Op,User_Perm_Op.permission_id==self.id)
        opstr = ','
        for op in ops: opstr = opstr + '%s,' % op.name
        return opstr
    def getOpIds(self):
        upos = store.find(User_Perm_Op,User_Perm_Op.permission_id==self.id)
        opstr = ','
        for upo in upos: opstr = opstr + '%d,' % upo.operation_id
        return opstr
    def getOps(self):
        tbs = [User_Perm_Op,Join(User_Op,User_Op.id==User_Perm_Op.operation_id)]
        return store.using(*tbs).find(User_Op,User_Perm_Op.permission_id==self.id)
class User_Op(object):
    __storm_table__ = "user_operation"
    id = Int(primary=True)
    name = Unicode(default=u'')
class User_Perm_Op(object):
    __storm_table__ = "user_permission_operation"
    id = Int(primary=True)
    permission_id = Int(default=1)
    permission = Reference(permission_id,User_Perm.id)
    operation_id = Int(default=1)
    operation = Reference(operation_id,User_Op.id)
class User_Info_Role(object):
    __storm_table__ = "user_info_role"
    id = Int(primary=True)
    user_id = Int(default=0)
    user = Reference(user_id,User_Info.id)
    role_id = Int(default=0)
    role = Reference(role_id,User_Role.id)
class User_Role_Perm(object):
    __storm_table__ = "user_role_permission"
    id = Int(primary=True)
    role_id = Int(default=0)
    role = Reference(role_id,User_Role.id)
    permission_id = Int(default=0)
    permission = Reference(permission_id,User_Perm.id)
    operation_id = Int(default=1)
    operation = Reference(operation_id,User_Op.id)

